﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
namespace iMapa.UI.View.Main.Wizard
{
    /// <summary>
    /// Interaction logic for WizardContainerControl.xaml
    /// </summary>
    /// 

    public partial class WizardContainerControl : UserControl
    {
        private int currentPage = 0;
        public ObservableCollection<UserControl> userControlCollection = new ObservableCollection<UserControl>();
        public WizardContainerControl()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(WizardContainerControl_Loaded);
        }

        void WizardContainerControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (userControlCollection == null)
                throw new Exception("You must set the userControlCollection variable with the set of UserControls in the wizard.");
            try { WizardContent.Content = userControlCollection[0]; }catch(Exception w ){}
            
        }
        private void DoEnable()
        {
            NextButton.IsEnabled = true;
            PreviousButton.IsEnabled = true;

        }
        private void NextButton_Click(object sender, RoutedEventArgs e)
        {
            FrameworkElement fe = (FrameworkElement)this.Parent;
            Window window = (Window)fe.Parent;
            IWizardStep wiz = (IWizardStep)userControlCollection[currentPage];
            if (!wiz.IsNextStepValid())
            {
                wiz.SetError();
                return;
            }

            //if finish
            if (currentPage == userControlCollection.Count - 1)
            {
                wiz.Finish();
                window.Close();
            }

            DoEnable();
            
            currentPage = currentPage + 1;
            if (currentPage >= userControlCollection.Count - 1)
            {
                currentPage = userControlCollection.Count - 1;
                nextbtnLabel.Text = "Finish";
                WizardContent.Content = userControlCollection[currentPage];
                //NextButton.IsEnabled = false;
                return;
            }
            WizardContent.Content = userControlCollection[currentPage];
        }

        private void PreviousButton_Click(object sender, RoutedEventArgs e)
        {
            DoEnable();
            currentPage = currentPage - 1;
            if (currentPage <= 0)
            {
                currentPage = 0;
                PreviousButton.IsEnabled = false;
                WizardContent.Content = userControlCollection[currentPage];
                return;
            }
            WizardContent.Content = userControlCollection[currentPage];

        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                FrameworkElement fe = (FrameworkElement)this.Parent;
                Window window = (Window)fe.Parent;
                window.Close();
            }
            catch (Exception)
            {
            }

        }
    }
    public interface IWizardStep{
        bool IsNextStepValid();
        void SetError();
        void Finish();
    }
}
